package com.guangyuan.network.interval;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.guangyuan.network.constants.SqlType;
import com.guangyuan.network.dao.DBFactory;
import com.guangyuan.network.dao.interfaces.sql.ProductGroupSql;
import com.guangyuan.network.dataobject.ProductGroup;
import com.guangyuan.network.datasource.pool.util.ConnectionUtil;
import com.guangyuan.network.external.ProductGroupOper;

public class ProductGroupCacche extends DefaultCache implements ProductGroupOper {

	private static ProductGroupCacche productGroupCache;
	private static Logger logger = Logger.getLogger(ProductGroupCacche.class);
	private static List<ProductGroup> productGroups = new ArrayList<ProductGroup>();

	private ProductGroupCacche() {

	}

	static {
		Connection conn = ConnectionUtil.getConnection();
		Object result = DBFactory.getProductDao().executeSql(conn,
				ProductGroupSql.selectSql, null, SqlType.OTHER);
		if (result instanceof ResultSet) {
			ResultSet rs = (ResultSet) result;
			if (null != rs) {
				try {
					while (rs.next()) {
						ProductGroup pG = new ProductGroup();
						pG.setId(rs.getInt("id"));
						pG.setName(rs.getString("name"));
						pG.setCnName(rs.getString("cn_name"));
						pG.setDescription(rs.getString("description"));
						pG.setGmtCreate(rs.getDate("gmt_create"));
						pG.setGmtModified(rs.getDate("gmt_modified"));
						pG.setOperator(rs.getString("operator"));
						productGroups.add(pG);
					}
				} catch (SQLException e) {
					logger.error(e.getMessage());
				} finally {
					ConnectionUtil.closeResultSet(rs);
					ConnectionUtil.closeConnection(conn);
				}
			}
		}

	}

	public static List<ProductGroup> listProductGroup() {
		return null;
	}

	@Override
	public boolean deleteProductGroup(String id) {
		int result = (Integer) execute(new String[]{id}, ProductGroupSql.deleteSql, SqlType.OTHER);
		if(result > 0) {
			for(ProductGroup pg : productGroups) {
				if(id.equals(pg.getId().toString())) {
					productGroups.remove(pg);
					break;
				}
			}
			return true;
		}
		return false;
	}

	// 由于现在的产品组比较少，所以现在的页数的功能就没有用
	@Override
	public List<ProductGroup> getGroupPerPage(int pageId) {

		return productGroups;
	}

	@Override
	public ProductGroup getProductGroupById(int id) {
		for (ProductGroup pG : productGroups) {
			if (pG.getId().intValue() == id) {
				return pG;
			}
		}
		return null;
	}

	@Override
	public boolean insertProductGroup(ProductGroup pG) {
		String[] parameters = new String[4];

		parameters[0] = pG.getName();
		parameters[1] = pG.getCnName();
		parameters[2] = pG.getDescription();
		parameters[3] = pG.getOperator();
		
		int rs = (Integer) execute(parameters, ProductGroupSql.insertSql, SqlType.INSERT);
		pG.setId(rs);
		pG.setGmtCreate(new Date());
		pG.setGmtCreate(new Date());
		
		productGroups.add(pG);
		
		return true;
	}

	@Override
	public boolean updateProductGroup(ProductGroup pG) {
		String[] parameters = new String[5];

		parameters[0] = pG.getName();
		parameters[1] = pG.getCnName();
		parameters[2] = pG.getDescription();
		parameters[3] = pG.getOperator();
		parameters[4] = pG.getId().toString();

		int result = (Integer) execute(parameters, ProductGroupSql.updateSql, SqlType.OTHER);
		if (result > 0) {
			updateLocatorGroup(pG);
			return true;
		}
		return false;
	}

	private static void updateLocatorGroup(ProductGroup pg) {
		for (ProductGroup p : productGroups) {
			if (p.getId().equals(pg.getId())) {
				p.setName(pg.getName());
				p.setCnName(pg.getCnName());
				p.setDescription(pg.getDescription());
				p.setOperator(pg.getOperator());
				break;
			}
		}
	}

	public static ProductGroupOper getInstance() {
		if (null == productGroupCache) {
			productGroupCache = new ProductGroupCacche();
		}
		return productGroupCache;
	}
	
}
